import { reactive, markRaw, ref, onMounted } from 'vue'
import { removeUndefinedParams } from '@/utils/index'
import { getInspectList } from '@/apis/taa/asset/inspect'
import { getDischargeUnitList } from '@/apis/taa/asset/account'
import type { CTableProps } from '@/components/CTable/index'
import type { BasicPagingListParams } from '@/apis/basicModel'
import type { SearchOptionsType, TableFieldsSettingType } from '@/components/CPageTable'

import { ColumnSettingTypeEnum } from '@/components/CPageTable'

export const useInspectList = () => {
  const dischargeUnitOption = ref<LabelValueOptions>([])

  async function fetchOptions() {
    try {
      const res = await getDischargeUnitList()
      const opts =
        res?.map((item) => {
          return {
            label: item.organizationName,
            value: item.organizationCode
          }
        }) || []
      dischargeUnitOption.value = opts
    } catch (error) {
      console.error('Error fetching options:', error)
      // 可以在这里处理错误，比如设置默认值或提示用户
    }
  }

  // 搜索字段及其他
  const searchForm = reactive({
    dischargeUnit: '',
    dischargeYear: ''
  })
  // 搜索配置
  const searchOptions: SearchOptionsType[] = markRaw([
    // {
    //   name: '排放单位',
    //   field: 'dischargeUnit',
    //   type: 'select',
    //   style: 'width: 200px',
    //   loadData: async (): Promise<LabelValueOptions> => {
    //     try {
    //       const res = await getDischargeUnitList();
    //       const opts =
    //         res?.map(item => {
    //           return {
    //             label: item.organizationName,
    //             value: item.organizationCode
    //           };
    //         }) || [];
    //       dischargeUnitOption.value = opts;
    //       return Promise.resolve(opts);
    //     } catch (error) {
    //       console.log('error', error);
    //       dischargeUnitOption.value = [];
    //       return Promise.resolve([]);
    //     }
    //   }
    // },
    {
      name: '年份',
      field: 'dischargeYear',
      type: 'datePicker',
      dateType: 'year',
      format: 'YYYY'
    }
  ])

  // 表格配置
  const tableOptions: CTableProps = {
    loadData,
    selection: false,
    border: false
  }

  function loadData(parameter: BasicPagingListParams) {
    const params = Object.assign(handleParams(), parameter)
    return getInspectList(params)
  }
  // 处理参数
  const handleParams = () => {
    let params = {
      dischargeUnit: searchForm.dischargeUnit,
      dischargeYear: searchForm.dischargeYear
    }
    params = removeUndefinedParams(params)
    return params
  }

  const tableFieldsSetting: TableFieldsSettingType[] = [
    {
      title: '排放单位',
      field: 'dischargeUnit',
      minWidth: 180,
      slot: 'dischargeUnitSolt',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.REQUIRED
      }
    },
    {
      title: '年份',
      field: 'dischargeYear',
      align: 'center',
      width: 80,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '核查排放量(tCO₂)',
      field: 'carbonInspectEmission',
      minWidth: 120,
      align: 'right',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '生产天数(天)',
      field: 'workDay',
      minWidth: 100,
      align: 'right',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '配额计算(tCO₂)',
      field: 'annualQuotaCalculate',
      minWidth: 120,
      align: 'right',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '配额缺口(tCO₂)',
      field: 'quotaGap',
      slot: 'quotaGapSlot',
      minWidth: 150,
      align: 'right',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '应发配额(tCO₂)',
      field: 'annualQuotaGrant',
      minWidth: 120,
      align: 'right',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      key: 'successNum',
      title: '履约进度',
      field: 'successNum',
      slot: 'successNumSlot',
      headerSlot: 'successNumHeaderSlot',
      minWidth: 120,
      align: 'right',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '操作',
      field: 'op',
      slot: 'op',
      width: 120,
      fixed: 'right',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.FIXED
      }
    }
  ]

  onMounted(() => {
    fetchOptions()
  })

  return {
    // 头部右侧筛选项配置
    searchOptions,
    // 筛选项字段
    searchForm,
    // 表格配置
    tableOptions,
    tableFieldsSetting,
    // 排放单位选项列表
    dischargeUnitOption
  }
}
